from wx import *
import webbrowser
import time

class OgFrame(Frame):
    def __init__(self, parent, id, title,app):
        Frame.__init__(self, parent, id, title, style= MAXIMIZE)
        self.init_size()

        #临时button
        #self.button = Button(self,pos=(1600,900),size=(100,100),label="关闭窗口")
        #self.button.Bind(EVT_BUTTON,self.close_window)

        #测试button
        #self.button_browser = Button(self,pos=(700,700),size=(100,100),label="打开浏览器")
        #self.button_browser.Bind(EVT_BUTTON,self.open_browser)


        #logo 和时间  测试

        logo = Image("软装到家icon.png", BITMAP_TYPE_ANY).Scale(500, 100 ).ConvertToBitmap()

        self.bitmap_button = BitmapButton(self,bitmap=logo,size=(500, 100  ),pos=(self.width_padding/2,self.height_padding/2 - 100 + self.base_height ))



        #测试
        self.Bind(EVT_ERASE_BACKGROUND,self.og_frame_updateui)

        #self.button.Bind(EVT_ERASE_BACKGROUND,self.og_button_updateui)

        #测试
        #bmp = Image("拎包入住底色.jpg", BITMAP_TYPE_ANY).Scale(100, 100).ConvertToBitmap()
        #self.bitmap_button = BitmapButton(self,bitmap=bmp,size=(100,100),pos=(800,200))
        #self.SetTransparent(200) #设置透明



        #第一块
        bmp = Image("软装到家底色.jpg", BITMAP_TYPE_ANY).Scale(self.ladder_width[1], self.ladder_height[1]).ConvertToBitmap()
        self.bitmap_button = BitmapButton(self,bitmap=bmp,size=(self.ladder_width[1], self.ladder_height[1]),pos=(self.width_padding/2,self.base_height + self.height_padding/2))

        #第二块
        bmp = Image("商学院底色.jpg", BITMAP_TYPE_ANY).Scale(self.ladder_width[2], self.ladder_height[1]).ConvertToBitmap()
        self.bitmap_button = BitmapButton(self,bitmap=bmp,size=(self.ladder_width[2], self.ladder_height[1]),pos=(self.width_padding/2 + self.ladder_width[1] + self.width_gutter,self.base_height + self.height_padding/2))

        #第三块
        bmp = Image("直播间底色.jpg", BITMAP_TYPE_ANY).Scale(self.ladder_width[2], self.ladder_height[1]).ConvertToBitmap()
        self.bitmap_button = BitmapButton(self,bitmap=bmp,size=(self.ladder_width[2], self.ladder_height[1]),pos=(self.width_padding/2 + self.ladder_width[1] + self.ladder_width[2] + self.width_gutter * 2,self.base_height + self.height_padding/2))

        #第四块
        bmp = Image("欧工简介底色.jpg", BITMAP_TYPE_ANY).Scale(self.ladder_width[2], self.ladder_height[1]).ConvertToBitmap()
        self.bitmap_button = BitmapButton(self,bitmap=bmp,size=(self.ladder_width[2], self.ladder_height[1]),pos=(self.width_padding/2, self.base_height + self.height_padding/2 + self.ladder_height[1] + self.height_gutter))

        #第五块
        bmp = Image("拎包入住底色.jpg", BITMAP_TYPE_ANY).Scale(self.ladder_width[2], self.ladder_height[1]).ConvertToBitmap()
        self.bitmap_button = BitmapButton(self,bitmap=bmp,size=(self.ladder_width[2], self.ladder_height[1]),pos=(self.width_padding/2 + self.ladder_width[2] + self.width_gutter, self.base_height + self.height_padding/2 + self.ladder_height[1] + self.height_gutter))

        #第六块
        bmp = Image("新闻资讯底色.jpg", BITMAP_TYPE_ANY).Scale(self.ladder_width[2], self.ladder_height[2]).ConvertToBitmap()
        self.bitmap_button = BitmapButton(self,bitmap=bmp,size=(self.ladder_width[2], self.ladder_height[2]),pos=(self.width_padding/2 + self.ladder_width[1] + self.width_gutter , self.base_height +  self.height_padding/2 + self.ladder_height[1] + self.height_gutter ))

        #第七块
        bmp = Image("品牌专区底色.jpg", BITMAP_TYPE_ANY).Scale(self.ladder_width[2], self.ladder_height[2]).ConvertToBitmap()
        self.bitmap_button = BitmapButton(self,bitmap=bmp,size=(self.ladder_width[2], self.ladder_height[2]),pos=(self.width_padding/2 + self.ladder_width[1] + self.ladder_width[2] + self.width_gutter*2, self.base_height + self.height_padding/2 + self.ladder_height[1] + self.height_gutter))

        #第八块
        bmp = Image("视频专区底色.jpg", BITMAP_TYPE_ANY).Scale(self.ladder_width[2], self.ladder_height[2]).ConvertToBitmap()
        self.bitmap_button = BitmapButton(self,bitmap=bmp,size=(self.ladder_width[2], self.ladder_height[2]),pos=(self.width_padding/2 + self.ladder_width[1] + self.width_gutter , self.base_height + self.height_padding/2 + self.ladder_height[1] + self.ladder_height[2] + self.height_gutter * 2 ))

        #第九块
        bmp = Image("大数据底色.jpg", BITMAP_TYPE_ANY).Scale(self.ladder_width[2], self.ladder_height[2]).ConvertToBitmap()
        self.bitmap_button = BitmapButton(self,bitmap=bmp,size=(self.ladder_width[2], self.ladder_height[2]),pos=(self.width_padding/2 + self.ladder_width[2]*3 + self.width_gutter*3,self.base_height +  self.height_padding/2 + self.ladder_height[1] + self.ladder_height[2] + self.height_gutter * 2))



        self.Show()


        #悬浮球
        float_frame_size =(self.pos_float_frame[2],self.pos_float_frame[3])
        #Image.AddHandler(PNGHandler);
        bmp = Image("small.png", BITMAP_TYPE_PNG).Scale(float_frame_size[0], float_frame_size[1]).ConvertToBitmap()

        self.float_frame =Frame(None, size = float_frame_size,style= STAY_ON_TOP|TRANSPARENT_WINDOW,pos=(self.pos_float_frame[0],self.pos_float_frame[1]))
        self.float_frame.SetBackgroundColour("black")
        self.float_frame_panel = Panel(self.float_frame,pos=(0,0),size=float_frame_size)
        self.float_frame_panel.SetBackgroundColour("black")
        self.float_frame_panel_button = BitmapButton(self.float_frame_panel,bitmap=bmp,pos=(0,0),size=float_frame_size)
        #self.float_frame_panel_button.Bind(EVT_BUTTON,self.toggle_frame)

        #self.float_frame_panel_button = BitmapButton(self,bitmap=bmp,pos=(self.pos_float_frame[0],self.pos_float_frame[1]),size=float_frame_size)
        self.float_frame_panel_button.Bind(EVT_MOUSE_EVENTS,self.drag_event)
        #self.float_frame.Bind(,self.drag_event)
        self.float_frame.Show()

        self.timer = Timer(self)
        self.Bind(EVT_TIMER, self.on_timer, self.timer)
        self.timer.Start(1000)

        #时间
        self.timer_text_button = Button(self,pos=(1500,self.height_padding/2 - 100 +self.base_height ),size=(100,100),label="",style=TRANSPARENT_WINDOW|BG_STYLE_TRANSPARENT)
        self.timer_text_button.SetTransparent(100)

        #self.button_browser.Bind(EVT_BUTTON,self.open_browser)

        #self.panel = Panel(self, id=ID_ANY, pos=(900,0), size=(100,100),style=TAB_TRAVERSAL, name=PanelNameStr)

    def on_timer(self, event):#显示时间事件处理函数
        t = time.localtime(time.time())
        ymd = time.strftime("%Y-%B-%d", t)
        ims = time.strftime("%H:%M:%S", t)
        #设置普通文本
        color = Colour(0,0,0,200)
        color2 = Colour(255,255,255,255)
        self.timer_text_button.SetLabel(ims)
        #self.timer_text_button.SetForegroundColour(color2)
        #self.timer_text_button.SetBackgroundColour(color)



    def init_size(self):
        InitAllImageHandlers()
        window_size = self.GetClientSize()
        self.window_width = window_size[0]
        self.window_height = window_size[1]
        #计算各个对象的位置
        #悬浮球的位置和大小 x y width height
        self.pos_float_frame = (self.window_width - 200,self.window_height-200,100,100)

        self.width_padding = 200
        self.height_padding = 200
        self.width_gutter = 10
        self.height_gutter = 10
        self.ladder_width  = (self.window_width-self.width_padding - self.width_gutter,(self.window_width-self.width_padding) / 2 - self.width_gutter, (self.window_width - self.width_padding) / 4 - self.width_gutter)
        self.ladder_height = (self.window_height-self.height_padding - self.height_gutter,(self.window_height-self.height_padding) / 2 - self.height_gutter , (self.window_height - self.height_padding) / 4 - self.height_gutter)
        self.base_height = 50


    def drag_event(self,event):
        if event.ButtonDown():
            self.start_position = event.GetPosition().Get()
            self.is_draging = False
            pass
        elif event.Dragging():
            self.drag_position = event.GetPosition().Get()
            diff_x = self.drag_position[0] - self.start_position[0]
            diff_y = self.drag_position[1] - self.start_position[1]
            position = self.float_frame.GetPosition().Get()
            pos = Point(position[0] + diff_x,position[1] + diff_y)
            self.float_frame.SetPosition(pos)
            self.is_draging = True
        elif event.ButtonUp():
            if(not self.is_draging):
                if self.IsIconized():
                    self.Iconize(False)
                else:
                    self.Iconize(True)
            self.is_draging = False
        pass


    def close_window(self,event):
        self.Close()


    def toggle_frame(self,event):
        if self.IsIconized():
            self.Iconize(False)
        else:
            self.Iconize(True)


    def open_browser(self,event):
        webbrowser.open("www.baidu.com")

    def og_frame_updateui(self,event):
        dc = event.GetDC()

        if not dc:
            dc = ClientDC(self)
        rect = self.GetUpdateRegion().GetBox()
        #dc.SetClippingRect(rect)
        #dc.Clear()
        print(self.GetClientSize())
        bmp = Image("mainback.jpg", BITMAP_TYPE_ANY).Scale(self.GetClientSize()[0], self.GetClientSize()[1]).ConvertToBitmap()
        dc.DrawBitmap(bmp, 0, 0)

    def og_button_updateui(self,event):
        dc = event.GetDC()

        if not dc:
            dc = ClientDC(self.button)
        rect = self.GetUpdateRegion().GetBox()
        #dc.SetClippingRect(rect)
        #dc.Clear()
        bmp = Image("欧工简介底色.jpg", BITMAP_TYPE_ANY).Scale(1000, 800).ConvertToBitmap()
        dc.DrawBitmap(bmp, 0, 0)


app = App()
OgFrame(None, 1, "欧工软装",app)
app.MainLoop()

